package cn.ekuma.epos.order;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.I_ProcessManager;
import cn.ekuma.epos.swing.panel.JHashTableEditor;
import com.openbravo.bean.erp.OrderProcessConfig;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.panels.JObjectListFinder;

public class JOrderTypeProcessEditor extends AbstractChildDAOJEditor<OrderProcessConfig,String> {
	private JTextField txtRuleName;
	private JHashTableEditor hashTableEditor;
	private I_ProcessManager processManager;
	private JButton btnSelectRule;
	private List<String> ruleNameList;
	private I_DataLogicERP dlSales;
	private JLabel label;
	private JTextField txtPriority;
	
	public JOrderTypeProcessEditor(DirtyManager dirty,final I_DataLogicERP dlSales,final I_ProcessManager processManager) {
		super(dirty);
		this.dlSales=dlSales;
		this.processManager=processManager;
		initComponents();
		txtRuleName.getDocument().addDocumentListener(dirty);
		txtPriority.getDocument().addDocumentListener(dirty);
		btnSelectRule.addActionListener(dirty);

		btnSelectRule.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JObjectListFinder<String> userList = JObjectListFinder.newJDialog(JOrderTypeProcessEditor.this);
	            String processName = (String)userList.showUserList(ruleNameList);
	            txtRuleName.setText(processName);
	            if(processName!=null)
					try {
						hashTableEditor.setHashMap(processManager.getOrderProcessProp(dlSales, obj.getOrderTypeId(), processName), false);
					} catch (BasicException e1) {
						e1.printStackTrace();
					}
			}
		});
	}
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		label = new JLabel("优先级：");
		panel.add(label);
		
		txtPriority = new JTextField();
		panel.add(txtPriority);
		txtPriority.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("处理过程");
		panel.add(lblNewLabel);
		
		txtRuleName = new JTextField();
		txtRuleName.setEditable(false);
		panel.add(txtRuleName);
		txtRuleName.setColumns(20);
		
	    btnSelectRule = new JButton("选择");
		panel.add(btnSelectRule);
		
		hashTableEditor=new JHashTableEditor();
		add(hashTableEditor, java.awt.BorderLayout.CENTER);
	}

	@Override
	public String getTabName() {
		return "外部单据处理插件";
	}
	
	@Override
	public void activate() throws BasicException {
		ruleNameList=processManager.getAllOrderProcess();
	}
	@Override
	public void refresh() {
	}

	@Override
	public OrderProcessConfig createNew(Properties prop) {
		OrderProcessConfig obj=new OrderProcessConfig();
		if(parentNode!=null)
			obj.setOrderTypeId(parentNode.getParentKey().getKey());
		return obj;
	}

	
	@Override
	public void rebuild(OrderProcessConfig obj) throws BasicException {
		obj.setProcessName(txtRuleName.getText());
		obj.setProp(hashTableEditor.getHashMap());	
		
		obj.setPriority((Integer) Formats.INT.parseValue(txtPriority.getText()));
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtPriority.setText(Formats.INT.formatValue(obj.getPriority()));
		txtRuleName.setText(obj.getProcessName());
		hashTableEditor.setHashMap(obj.getProp(), false);
		btnSelectRule.setEnabled(editAble);
		txtPriority.setEditable(editAble);
	}

	@Override
	public Class getEditClass() {
		return OrderProcessConfig.class;
	}

}
